package com.zizo.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import org.nlpcn.commons.lang.util.WordAlert;

public class Analysis {

	/**
	 * @Title: removeFullWidth
	 * @Description: TODO 全角转为半角,大写
	 * @return String
	 * @author HeYan
	 */
	public static String removeFullWidth(String data) {
		char[] result = WordAlert.alertStr(data.trim());
		data = new String(result);
		return data.toUpperCase();
	}

	final static String pattern_1 = "\\?{1,}|？{1,}";
	final static Pattern p_1 = Pattern.compile(pattern_1);
	final static String pattern_2 = "\n{1,}";
	final static Pattern p_2 = Pattern.compile(pattern_2);
	final static String pattern_3 = "[，。！!；,;]{1,}";
	final static Pattern p_3 = Pattern.compile(pattern_3);
	final static String pattern_4 = "[\\. ~\\-\\+\\*><]{2,}";
	final static Pattern p_4 = Pattern.compile(pattern_4);
	final static String pattern_5 = "[ �　\\s+]";
	final static Pattern p_5 = Pattern.compile(pattern_5);
	final static String pattern_6 = "(http://|ftp://|https://|www){0,1}[^\u4e00-\u9fa5\\s]*?\\.(com|net|cn|me|tw|fr)[^\u4e00-\u9fa5\\s]*";
	final static Pattern p_6 = Pattern.compile(pattern_6.toUpperCase());

	public static String stringFilter(String data) {
		// 英文? OR 中文？>1 exchange ?OR？
		Matcher m_1 = p_1.matcher(data);
		String res_1 = m_1.replaceAll("?");
		// \n >1 exchange \n
		Matcher m_2 = p_2.matcher(res_1);
		String res_2 = m_2.replaceAll("\n");
		// 连续>=1替换
		Matcher m_3 = p_3.matcher(res_2);
		String res_3 = m_3.replaceAll("}{");
		// 连续>=2替换
		Matcher m_4 = p_4.matcher(res_3);
		String res_4 = m_4.replaceAll("}{");
		// 连续>=2替换
		Matcher m_5 = p_5.matcher(res_4);
		String res_5 = m_5.replaceAll("");
		// 替换网址
		Matcher m_6 = p_6.matcher(res_5);
		String res_6 = m_6.replaceAll("");
		return res_6;
	}

	/**
	 * @Title: replaceSyn
	 * @Description: TODO 近义词替换,传入要清洗的数据和替换的近义词
	 * @return String
	 * @author HeYan
	 */
	public static String replaceSyn(String data, Map<String, String> sys) {
		data = removeFullWidth(data);
		data = stringFilter(data);
		for (Map.Entry<String, String> entry : sys.entrySet()) {
			data = data.replace(entry.getKey(), entry.getValue());
		}
		return data;
	}

	public static Map<Integer, Integer> getAvgWord(String data, Map<Integer, Integer> map) {
		try {
			String[] content = data.trim().split("\n");
			for (int i = 0; i < content.length; i++) {
				int length = content[i].length();
				if (map.containsKey(length)) {
					int temp = map.get(length) + 1;
					map.put(length, temp);
				} else {
					map.put(length, 1);
				}
			}
		} catch (Exception e) {
		}
		return map;
	}

	// 解压缩
	public static String uncompress(String str) throws IOException {
		if (str == null || str.length() == 0) {
			return str;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("UTF-8"));
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[256];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		// toString()使用平台默认编码，也可以显式的指定如toString(&quot;GBK&quot;)
		return out.toString();
	}

	public static void main(String[] args) {
		System.out.println();
	}
}
